<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/7/10 0010
 * Time: 17:10
 */

namespace App\Content\Controllers\Admin;

use App\Base\Controllers\Controller;
use App\Content\Contracts\ArticleClassInterface;
use App\Content\Contracts\ArticleInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class ArticleController extends Controller
{

    /**
     * @return ArticleClassInterface
     */
    public function getArticleClassInterface()
    {
        return app()->make(ArticleClassInterface::class);
    }

    /**
     * @return ArticleInterface
     */
    public function getArticleInterface()
    {
        return app()->make(ArticleInterface::class);
    }

    /**
     * 新闻首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            $where = function ($query) use ($request) {
                $title = $request->input("title");
                if (!empty($title)) {
                    $query->where("title", "like", '%' . $title . '%');
                }
                $status = $request->input("status", "");
                if ($status != '') {
                    $query->where("status", $status);
                }
            };
            return $this->getArticleInterface()->getArticleList($where);
        }
        $menuList = $this->getArticleClassInterface()->getClassMenuList([]);
        $menuArr = array();
        foreach ($menuList as $k => $item) {
            $menuArr[$k]["id"] = $item->id;
            $menuArr[$k]["name"] = $item->name;
            $menuArr[$k]["pId"] = $item->pid;
        }
        return view("content.article.index")->with("menu_list", json_encode($menuArr));
    }

    /**
     * 新闻新增
     */
    public function add(Request $request)
    {

        if ($request->getMethod() == "POST") {
           return  $this->getArticleInterface()->saveData($request->all());
        }

        $category_id = $request->input('category_id');
        if (empty(intval($category_id))) {
            return   $this->show_error('请先在页面左侧选择文章分类');
        } else {
            //加载模板
            return view("content.article.add")->with("category_id",$category_id);
        }

    }

    public function edit(Request $request){

        $id=$request->input("id");
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'title' => 'required',
                'body' => 'required',
            ],["title.required"=>'标题必须有值',"body.required"=>'标题必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return  $this->getArticleInterface()->saveData($request->all());
        }
        $data=$this->getArticleInterface()->getInfo($id);
        return view("content.article.edit")->with("data",$data);
    }

    /**
     * 广告删除
     * @param Request $request
     */
    public function del(Request $request){
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'id' => 'required',
            ],["id.required"=>'删除参数id是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $where=function($query) use ($request){
                $id=$request->input("id");
                $query->where("id",$id);
            };
            return  $this->getArticleInterface()->deleteData($where);
        }
    }

    /**
     * 改变状态
     * @param Request $request
     */
    public function changeStatus(Request $request){
        return  $this->getArticleInterface()->saveData($request->all());
    }
}